//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.app.service;

import com.ruoyi.live.base.domain.LiveServer;
import com.ruoyi.live.base.domain.LiveTaskUserChannel;
import com.ruoyi.live.base.mapper.LiveServerMapper;
import com.ruoyi.live.base.mapper.LiveTaskUserChannelMapper;
import com.ruoyi.live.tool.commons.IConvert;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class VideoService {
    private static final Logger log = LoggerFactory.getLogger(VideoService.class);
    @Autowired
    LiveTaskUserChannelMapper channelDao;
    @Autowired
    LiveServerMapper liveServerDao;
    private final Float MB_1 = 1048576.0F;

    public VideoService() {
    }

    @Async
    public void getLiveVideoInfo(Long channelId, String filePath, Long serverId, String channelName) {
        LiveServer server = (LiveServer)this.liveServerDao.selectById(serverId);
        File file = new File(filePath + server.getFilePath() + channelName + ".flv");
        Boolean correct = true;

        try {
            Thread.sleep(2000L);
        } catch (InterruptedException var11) {
            var11.printStackTrace();
        }

        if (file.exists()) {
            Long fileKb = file.length();
            if (fileKb > 1024L) {
                try {
                    Float mbCount = (float)fileKb / this.MB_1;
                    this.setLiveVideoInfo(channelId, (Integer)null, mbCount.intValue(), 2);
                    correct = false;
                } catch (Exception var10) {
                    var10.printStackTrace();
                    log.error("视频信息获取失败", var10);
                }
            }
        }

        if (correct) {
            this.setLiveVideoInfo(channelId, (Integer)null, (Integer)null, 1);
        }

    }

    public Integer getScreenVideoInfo(Long channelId, String filePath, String channelName) {
        File file = new File(filePath + "/static/static/data/record/" + channelName + ".mp4");
        if (file.exists()) {
            Long fileKb = file.length();
            if (fileKb > 2 * 1024l * 1024L) {
                try {
                    Encoder encoder = new Encoder();
                    String length = "";
                    MultimediaInfo m = encoder.getInfo(file);
                    Long ls = m.getDuration() / 1000L;
                    if (!(new Long(0L)).equals(ls)) {
                        Float mbCount = (float)fileKb / this.MB_1;
                        this.setScreenVideoInfo(channelId, IConvert.long2int(ls), mbCount.intValue(), 2);
                        return 2;
                    }
                } catch (Exception var11) {
                    log.error("视频信息获取失败", var11);
                }
            }
        }

        this.setScreenVideoInfo(channelId, (Integer)null, (Integer)null, 2);
        return 3;
    }

    public void setLiveVideoInfo(Long channelId, Integer length, Integer size, Integer state) {
        this.channelDao.updateById((new LiveTaskUserChannel()).setId(channelId).setLiveVideoLength(length).setLiveVideoSize(size).setLiveVideoState(state));
    }

    public void setScreenVideoInfo(Long channelId, Integer length, Integer size, Integer state) {
        this.channelDao.updateById((new LiveTaskUserChannel()).setId(channelId).setScreenVideoLength(length).setScreenVideoSize(size).setScreenVideoState(state));
    }

    public static void main(String[] args) throws EncoderException {
        String filepath = "D://fail.mp4";
        File file = new File(filepath);
        Encoder encoder = new Encoder();
        String length = "";
        MultimediaInfo m = encoder.getInfo(file);
        Long ls = m.getDuration() / 1000L;
        System.out.println(ls.intValue());
    }
}
